package com.tianji.learning.controller;

import com.tianji.common.domain.dto.PageDTO;
import com.tianji.common.domain.query.PageQuery;
import com.tianji.common.utils.UserContext;
import com.tianji.learning.domain.dto.LearningPlanDTO;
import com.tianji.learning.domain.po.LearningLesson;
import com.tianji.learning.domain.vo.LearningLessonVO;
import com.tianji.learning.domain.vo.LearningPlanPageVO;
import com.tianji.learning.service.ILearningLessonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * <p>
 * 学生课表 控制器
 * </p>
 *
 * @author xjs
 */
@Api(tags = "LearningLesson管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/lessons")
public class LearningLessonController {

    private final ILearningLessonService learningLessonService;


    /**
     * 我的课表分页查询
     * */
    @ApiOperation("我的课表分页查询")
    @GetMapping("/page")
    public PageDTO<LearningLessonVO> page(PageQuery query){
        return learningLessonService.pageQuery(query);
    }

    /**
     * 查询正在学习的课程
     * */
    @ApiOperation("查询正在学习的课程")
    @GetMapping("/now")
    public LearningLessonVO now(){
        return learningLessonService.now();
    }

    /**
     * 删除课程
     * */
    @ApiOperation("删除课程")
    @DeleteMapping("/{courseId}")
    public void delete(@PathVariable("courseId")Long courseId){
        learningLessonService.removeUserCourses(UserContext.getUser(), Arrays.asList(courseId));
    }

    /**
     * 查询用户课表中指定课程状态
     * */
    @ApiOperation("查询用户课表中指定课程状态")
    @GetMapping("/{courseId}")
    public LearningLesson getCourseStatus(@PathVariable("courseId")Long courseId){
        return learningLessonService.getCourseStatus(UserContext.getUser(), courseId);
    }

    /**
     * 检查课程是否有效（feign）
     * */
    @ApiOperation("检查课程是否有效（feign）")
    @GetMapping("/{courseId}/valid")
    public Long isLessonValid(@PathVariable("courseId") Long courseId){
        return learningLessonService.queryUserLessonByCourseId(UserContext.getUser(), courseId);
    }

    /**
     * 统计课程学习人数
     * @param courseId 课程id
     * @return 学习人数
     */
    @ApiOperation("统计课程学习人数（feign）")
    @GetMapping("/{courseId}/count")
    public Integer countLearningLessonByCourse(@PathVariable("courseId") Long courseId){
        return learningLessonService.countLearningLessonByCourse(courseId);
    }

    /**
     * 创建学习计划
     *
     */
    @PostMapping("/plans")
    public void plans(@RequestBody LearningPlanDTO dto){
        learningLessonService.plan(dto);
    }


    @GetMapping("/plans")
    public LearningPlanPageVO getPlans(PageQuery query){
        return learningLessonService.getPlans(query);
    }

}
